package org.scboy.dbms.utils;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import cn.hutool.core.util.StrUtil;

public class JavaScriptUtil{

	/**
	 * 格式化数据
	 * @param data 数据对象
	 * @param scriptExp js表达式
	 * @return
	 */
	public static Object fomartReturnObj(String scriptExp,String function,Object ...params){
		Object result=null;
		if (StrUtil.isNotBlank(scriptExp)) {
			// 调用js解析。
			ScriptEngineManager manager = new ScriptEngineManager();
			ScriptEngine engine = manager.getEngineByName("js");
			try {
				engine.eval(scriptExp);
				Invocable invocable = (Invocable) engine;
				result = invocable.invokeFunction(function,params);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return result;
	}
}
